﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Semantics;
using EOS.Security;

public partial class ConnectNodes :WikiPageBase 
{
   
    Node outNode;
    Node inNode;
    string flagString;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
            return;
        string nodeidString = Request.QueryString["NodeId"];
        flagString = Request.QueryString["Flag"];
        if (nodeidString == null)
        {
            base.ShowError("No parameter NodeId");
            return;
        }
        if (flagString == null)
        {
            base.ShowError("No parameter Flag");
            return;
        }
        int nodeid = -1;        
        try
        {
            nodeid = int.Parse(nodeidString);           
        }
        catch (Exception ex)
        {
            base.ShowError(ex.ToString());
        } 
        //显示要关联的基节点
        if (flagString == "1")//基节点是out节点
        {
            outNode = base.GraphDatabase.QueryNode(nodeid);
            if (outNode == null) return;
            this.OutNodeText .Text = outNode.Label;
            this.OutNodeLabel.Text = outNode.Label;
            this.HiddenFieldOutId.Value = nodeid.ToString ();
            this.OutNodeText.ReadOnly = true;           
            this.InNodeText.ReadOnly = false;
            
        }
        if(flagString =="0")//基节点是in节点
        {
            inNode = base.GraphDatabase.QueryNode(nodeid);
            if (inNode == null) return;
            this.InNodeText.Text = inNode.Label;
            this.InNodeLabel.Text = inNode.Label;
            this.HiddenFieldInId.ID = nodeid.ToString();
            this.InNodeText.ReadOnly = true;
            this.OutNodeText.ReadOnly = false;
        }
     
    }
    /// <summary>
    /// 确定建立关联边
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ButtonSure_Click(object sender, EventArgs e)
    {
        if (this.OutNodeLabel.Text.Trim() == "" || this.InNodeLabel.Text.Trim() == "" ||
            this.RelationNodeLabel.Text.Trim() == "")
        {
            base.ShowError("Rassure all Node Infomation!");
            return;
        }
        Node outNode=new Node (int.Parse (this.HiddenFieldOutId .Value.ToString().Trim ()),this.OutNodeLabel.Text.ToString ().Trim ());
        Node inNode=new Node (int.Parse (this.HiddenFieldInId.Value.ToString().Trim ()),this.InNodeLabel.Text.ToString ().Trim ());
        Node relationNode = new Node(int.Parse(this.HiddenFieldRelationId .Value.ToString().Trim()), this.RelationNodeLabel .Text.ToString().Trim());
        Edge edge= base.GraphDatabase.ConnectNode(outNode, relationNode, inNode);
        if (edge == null)
        {
            base.ShowError("Connect Node Failed!!");
        }
        else
        {
            base.ShowInfo("Connect Node Successed");
        }
                      
    }
   


    /// <summary>
    /// 根据文本解析Out节点
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ButtonResolveOut_Click(object sender, EventArgs e)
    {
        if (this.OutNodeText .Text.ToString() == "")
        {
            base.ShowError("Out Text Can't be Empty!");
            return;
        }
        List<Node> outNode = base.GraphDatabase.QueryNodes(this.OutNodeText.Text.ToString().Trim());
        if (outNode.Count < 1)
        {
            base.ShowError("No thus Node,Please resolve again!");
            return;
        }
        foreach (Node node in outNode )
        {
            ListItem item=new ListItem (node.Label,node .Id .ToString ());
            this.OutNodeListBox.Items.Add(item);
        }
    }

    /// <summary>
    /// 通过对话框选择节点
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ButtonSelectOut_Click(object sender, EventArgs e)
    {
        ListItem item = this.OutNodeListBox.SelectedItem;
        if (item == null)
        {
            base.ShowError("Please choose a node");
            return;
        }
        this.OutNodeLabel.Text = item.Text;
        this.HiddenFieldOutId.Value = item.Value;

    }
    /// <summary>
    /// 选择作为Relation的节点
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ButtonResolveRelation_Click(object sender, EventArgs e)
    {

        if (this.RelationNodeText.Text.ToString() == "")
        {
            base.ShowError("Relation Text Can't be Empty!");
            return;
        }
       List< Node> relationNode = base.GraphDatabase.QueryNodes(this.RelationNodeText.Text.ToString().Trim());
       if (relationNode.Count < 1)
       {
           base.ShowError("No thus Node,Please resolve again!");
           return;
       }
       foreach (Node node in relationNode)
       {
           ListItem item = new ListItem(node.Label, node.Id.ToString());
           this.RelationNodeListBox .Items.Add(item);
       }

    }
    protected void ButtonSelectRelation_Click(object sender, EventArgs e)
    {
        ListItem item = this.RelationNodeListBox .SelectedItem;
        if (item == null)
        {
            base.ShowError("Please choose a node");
            return;
        }
        this.RelationNodeLabel .Text = item.Text;
        this.HiddenFieldRelationId .Value = item.Value;
    }
    /// <summary>
    /// 根据文本解析In节点
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ButtonResolveIn_Click(object sender, EventArgs e)
    {
        if (this.InNodeText .Text.ToString() == "")
        {
            base.ShowError("In Text Can't be Empty!");
            return;
        }
        List<Node> inNode = base.GraphDatabase.QueryNodes(this.InNodeText .Text.ToString().Trim());
        if (inNode.Count < 1)
        {
            base.ShowError("No thus Node,Please resolve again!");
            return;
        }
        foreach (Node node in inNode )
        {
            ListItem item = new ListItem(node.Label, node.Id.ToString());
            this.InNodeListBox .Items.Add(item);
        }
    }
    protected void ButtonSelectSelectIn_Click(object sender, EventArgs e)
    {
        ListItem item = this.InNodeListBox .SelectedItem;
        if (item == null)
        {
            base.ShowError("Please choose a node");
            return;
        }
        this.InNodeLabel .Text = item.Text;
        this.HiddenFieldInId .Value = item.Value;
    }
}